/**
 * 
 */
package com.wzl.ma.db;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * @author Wu Wei
 * 
 */
public class MaDbAdapter {

	public static final String DATABASE_NAME = "ma";
	public static final String ILLNESS_TABLE_NAME = "illness";
	public static final String MEDICINE_TABLE_NAME = "medicine";
	public static final String TAKING_TABLE_NAME = "taking";
	public static final String USERINFORMATION_TABLE_NAME = "userInformation";

	public static final int DATABASE_VERSION = 1;

	public static final String CREATE_ILLNESS_TABLE = "create table "
			+ ILLNESS_TABLE_NAME
			+ " (Iid integer primary key autoincrement, IllnessName text);";

	public static final String CREATE_MEDICINE_TABLE = "create table "
			+ MEDICINE_TABLE_NAME
			+ " (Mid integer primary key autoincrement, Iid integer, MedicineName text, Amout integer, Unit text, PhotoPath text, AudioPath text, PrescriptionDate text);";

	public static final String CREATE_TAKING_TABLE = "create table "
			+ TAKING_TABLE_NAME
			+ " (Tid integer primary key autoincrement, Mid integer, TakeDate text, TakeTime text, TakeAmount integer, IsDone integer);";

	public static final String CREATE_USERINFORMATION_TABLE = "create table "
			+ USERINFORMATION_TABLE_NAME
			+ " (Password text, MedicalRecord text, Allergy text, BreakfastTime text, LunchTime text, DinnerTime text, UseESMS integer, EContactNum text, EMessage text, DelayTheshold integer);";

	private final Context context;
	private SQLiteDatabase db;
	private DbHelper dbHelper;

	public MaDbAdapter(Context ctx) {
		this.context = ctx;
	}

	public MaDbAdapter open() throws SQLException {
		this.dbHelper = new DbHelper(this.context);
		this.db = this.dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		this.dbHelper.close();
	}

	public SQLiteDatabase getDatabase() {
		return this.db;
	}

	public boolean hasUserInformation() {
		String[] columns = { "Password" };
		Cursor cursor = this.db.query(this.USERINFORMATION_TABLE_NAME, columns,
				null, null, null, null, null);
		int count = cursor.getCount();
		return (count > 0);
	}

}
